package com.thread.test;

public class VolatileVisibilitySample {

    boolean initFlag = false;

    public void save(){
        this.initFlag = true;
        String threadName = Thread.currentThread().getName();
        System.out.println(threadName +" 线程修改了initFlag");
    }

    public void load(){
        String threadName = Thread.currentThread().getName();
        while(!initFlag){

        }
        System.out.println(threadName + "线程感知到initFlag被修改了");
    }

    public static void main(String[] args) throws InterruptedException {
        VolatileVisibilitySample sample = new VolatileVisibilitySample();
        Thread threadA = new Thread(() -> {
            sample.save();
        }, "threadA");
        Thread threadB = new Thread(() -> {
            sample.load();
        }, "threadB");
        threadB.start();
        Thread.sleep(1000);
        threadA.start();
    }

}
